package net.wangds.yunz.initialize.flow;

import net.wangds.auth.resource.comp.GeneralResourceComp;
import net.wangds.common.base.funcs.Functions;
import net.wangds.config.comp.SysConfComp;
import net.wangds.config.module.Config;
import net.wangds.jpa.Sorts;
import net.wangds.log.helper.LogHelper;
import net.wangds.procengine.ProcResEnum;
import net.wangds.procengine.flow.FlowContext;
import net.wangds.procengine.flow.define.FlowDef;
import net.wangds.procengine.flow.define.actor.ActorDef;
import net.wangds.procengine.flow.define.node.FlowNode;
import net.wangds.procengine.flow.define.node.JavaFlowNode;
import net.wangds.procengine.flow.instance.actor.Actor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import java.util.function.BiFunction;

/**
 * .
 * <p></p>
 *
 * @author 王东石 2020/10/18 9:53.
 */
@Service
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public class StepInitModules extends JavaFlowNode {

    public static final String CONFIG_MENU_LABLE_CATA = "menu_label";

    public static final String KEY_MODS = "MODS";

    @Autowired
    private SysConfComp mmCmpConf;

    @Autowired
    private GeneralResourceComp mmCmpRes;

    public <A extends ActorDef> StepInitModules(FlowDef<A> flow,
                                                BiFunction<FlowContext, Actor, ProcResEnum> func,
                                                FlowNode previousNode) {
        super(flow, func, previousNode);
    }

    public <A extends ActorDef> StepInitModules(FlowDef<A> flow, FlowNode previousNode) {
        super(flow, previousNode);
        this.setFunc(this::proc);
    }

    private ProcResEnum proc(FlowContext ctx, Actor actor) {
        LogHelper.dev("清理现有模块");
        //删除原有配置
        mmCmpConf.findByCata(CONFIG_MENU_LABLE_CATA, Sorts.createTm_asc).forEach(mmCmpConf::delete);

        LogHelper.dev("初始化现有模块");
        Functions.ofList(
                //监控和告警模块
                Config.of(CONFIG_MENU_LABLE_CATA, "monitor","监控和告警", 0, "")
                ,Config.of(CONFIG_MENU_LABLE_CATA, "monitor_map","监控地图", 0, "")
                ,Config.of(CONFIG_MENU_LABLE_CATA, "monitor_data","监控数据", 0, "")
                ,Config.of(CONFIG_MENU_LABLE_CATA, "monitor_info","基础数据", 0, "")

                //专家库模块
                ,Config.of(CONFIG_MENU_LABLE_CATA, "problems","专家库", 0, "")
                ,Config.of(CONFIG_MENU_LABLE_CATA, "problems_mng","问题管理", 0, "")


                //备品备件模块
                ,Config.of(CONFIG_MENU_LABLE_CATA, "spareparts","备品备件", 0, "")
                ,Config.of(CONFIG_MENU_LABLE_CATA, "spareparts_mng","备品管理", 0, "")
                ,Config.of(CONFIG_MENU_LABLE_CATA, "spareparts_check","库存盘点", 0, "")

                //系统管理模块
                ,Config.of(CONFIG_MENU_LABLE_CATA, "console","系统管理", 0, "")
                //,Config.of(CONFIG_MENU_LABLE_CATA, "console_news","资讯管理", 0, "")
                ,Config.of(CONFIG_MENU_LABLE_CATA, "console_system","系统管理", 0, "")
                ,Config.of(CONFIG_MENU_LABLE_CATA, "console_config","系统设置", 0, "")
                ,Config.of(CONFIG_MENU_LABLE_CATA, "console_app"," APP设置", 0, "")

                //大屏模块
                ,Config.of(CONFIG_MENU_LABLE_CATA, "bigscreen","大屏", 0, "")
        ).forEach(
                c-> mmCmpConf.findByCataAndItem(c.getCata(), c.getItem()).orElseGet(()-> mmCmpConf.save(c))
        );

        ctx.putBean(KEY_MODS, mmCmpConf.findByCata(CONFIG_MENU_LABLE_CATA, Sorts.createTm_asc));

        return ProcResEnum.CONTINUE;
    }


}
